//
//  SYMyFavoriteViewController.m
//  BSHOUYIFANG
//
//  Created by lanou3g on 15/8/21.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYMyFavoriteViewController.h"
#import "Header.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#import "SYMyHomeFavouriteModel.h"
#import <UIImageView+WebCache.h>
#import "SYFavouriteTableViewCell.h"
#import "SYDetailViewController.h"
#import "SYFileHandle.h"

@interface SYMyFavoriteViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *mainTV;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)FMDatabase *db;
@end

@implementation SYMyFavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationItemTitleColor];
    _dataArr = [[NSMutableArray alloc]initWithCapacity:0];
    _mainTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _mainTV.dataSource = self;
    _mainTV.delegate = self;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.view addSubview:_mainTV];
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filrPath = [str stringByAppendingPathComponent:@"like.db"];
    _db = [FMDatabase databaseWithPath:filrPath];
    if (![_db open]) {
        NSLog(@"%@",filrPath);
    }
    [self parserData];
    UIButton*leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30*0.6,30*0.6)];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem= leftItem;
}
- (void)searchprogram{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"我喜欢的"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = customLab;
}

   //*************开启编辑*************//

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    _mainTV.editing = YES;
    [super setEditing:editing animated:animated];
    [self.view.subviews[0] setEditing:editing animated:animated];
}

   //*************编辑结束后发送通知*************//

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    SYMyHomeFavouriteModel *model = _dataArr[indexPath.row];
    [_dataArr removeObjectAtIndex:indexPath.row];
    [_db executeUpdate:@"delete from like where backuptitle = ?",model.titlel];
    [_mainTV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    NSNotification *notif = [[NSNotification alloc]initWithName:@"cancelLike" object:nil userInfo:nil];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotification:notif];
}


    //*************读取数据库*************//

- (void)parserData{
    NSString *userName = [[SYFileHandle shareInstance] phoneNumber];
    FMResultSet *result = [_db executeQuery:@"select urlstr, backuptitle, backupimg from like where username = ?",userName];
    while ([result next]) {
        SYMyHomeFavouriteModel *model = [[SYMyHomeFavouriteModel alloc]init];
        model.titlel = [result stringForColumn:@"backuptitle"];
        model.img = [result stringForColumn:@"backupimg"];
        model.urlStr = [result stringForColumn:@"urlstr"];
        [_dataArr addObject:model];
    }
}


    //*************tableview datasource*************//

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SYFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[SYFavouriteTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    SYMyHomeFavouriteModel *model = _dataArr[indexPath.row];
    cell.titleLabel.text = model.titlel;
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:model.img]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

   //*************点击跳转事件*************//

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SYMyHomeFavouriteModel *model = _dataArr[indexPath.row];
    SYDetailViewController *deVC = [[SYDetailViewController alloc]init];
    deVC.urlStr = model.urlStr;
    deVC.backupTitle = model.titlel;
    deVC.backupImg = model.img;
    [self.navigationController pushViewController:deVC animated:YES];
}

   //*************实时更新数据*************//

- (void)viewWillAppear:(BOOL)animated{
    [_dataArr removeAllObjects];
    [self parserData];
    [_mainTV reloadData];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
